package com.moweilin.mo_gateway.servers;

import ch.qos.logback.classic.Level;
import com.moweilin.mo_gateway.StartCommand;
import com.moweilin.mo_gateway.handlers.*;
import com.moweilin.mo_gateway.services.IHandlerService;
import com.moweilin.mo_gateway.services.impl.SimpleModuleService;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.vertx.core.http.HttpServerOptions.DEFAULT_HOST;
import static io.vertx.core.http.HttpServerOptions.DEFAULT_PORT;


/**
 * Created by moweilin on 2017/1/27.
 */
public class MoGatewayServer extends AbstractVerticle{
    private Logger logger = LoggerFactory.getLogger(MoGatewayServer.class);

    private Integer port;
    private String host;
    private Boolean alpn;
    private IHandlerService handlerService;
    private Handler<HttpServerRequest> dispatcherHandler;
    private IFailureHandler<HttpServerRequest> internalFailureHandler;

    protected Handler<HttpServerRequest> buildDispatcherHandler(Vertx vertx){
        Handler<HttpServerRequest> localRequestHandler =  new DefaultLocalRequestHandler(vertx);
        HttpClient httpClient = vertx.createHttpClient();
        IModuleRequestHandler moduleRequestHandler = new DefaultModuleRequestHandler(httpClient);
        return new DefaultDispatcherHandler(SimpleModuleService.getInstance(),localRequestHandler,moduleRequestHandler);
    }
    protected IFailureHandler<HttpServerRequest> buildInternalFailureHandler(Vertx vertx){
        return new SimpleInternalFailureHandler();
    }

    public void start(){
        dispatcherHandler = buildDispatcherHandler(vertx);
        internalFailureHandler = buildInternalFailureHandler(vertx);
        HttpServerOptions options = new HttpServerOptions();
        Boolean debug = config().getBoolean(StartCommand.DEBUG_KEY,false);
        if(debug){
            ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.moweilin.mo_gateway");
            rootLogger.setLevel(Level.DEBUG);
        }
        //alpn
        alpn = config().getBoolean("alpn",false);

        HttpServer httpServer = vertx.createHttpServer(options);
        //port
        port = config().getInteger("port",DEFAULT_PORT);

        //host
        host = config().getString("host", DEFAULT_HOST);
        //
        String defaultModule = config().getString("defaultModule","main");


        httpServer.requestHandler(req->{

            try {
                dispatcherHandler.handle(req);
            }catch (Exception e){
                internalFailureHandler.handle(req,e);
            }

        });
        httpServer.listen(port,host,ar->{
            if(ar.succeeded()) {
                logger.info("start servers listening at " + host + ":"+ port);
            }
        });
    }


}
